﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

public partial class displayresult : System.Web.UI.Page
{
    /// <summary>
    /// 索引显示页
    /// </summary>
    private string path_tmp;
    private string title_tmp;
    public string str4 = AppDomain.CurrentDomain.BaseDirectory;
    private string text;
    

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection sc2 = new SqlConnection(Application["sqlconnection"].ToString());
        Label2.Text = "hi, " + Application["user_name"].ToString();
        //Label1.Text = Request.Params["path"].ToString()+Request.Params["title"].ToString();
        if (Request.Params["path"].ToString() != "null")
        {
            title_tmp = Request.Params["title"].ToString();
            path_tmp = Request.Params["path"].ToString();
            //MessageBox.Show("wrong direction.");
        }
        else
        {
            title_tmp = Request.Params["title"].ToString();
            sc2.Open();
            //MessageBox.Show("Database opened.");
            SqlCommand com_getpath = sc2.CreateCommand();
            com_getpath.CommandText = "select Path from Suoyin where Title = @title";
            com_getpath.Parameters.Add("@title", SqlDbType.Text);
            com_getpath.Parameters["@title"] = new SqlParameter("@title", title_tmp);
            SqlDataReader sdr=com_getpath.ExecuteReader();
            while (sdr.Read())
                path_tmp = sdr[0].ToString();
            sc2.Close();
        }

        sc2.Open();
        SqlCommand com_savecookies = sc2.CreateCommand();
        string query = Application["query"].ToString();
        string name = Application["user_name"].ToString();
        string time = DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString();
        com_savecookies.CommandText = "INSERT INTO Cookies VALUES(@name,@time,@query,@title)";

        com_savecookies.Parameters.Add("@name", SqlDbType.Text);
        com_savecookies.Parameters.Add("@time", SqlDbType.Text);
        com_savecookies.Parameters.Add("@query", SqlDbType.Text);
        com_savecookies.Parameters.Add("@title", SqlDbType.Text);

        com_savecookies.Parameters["@name"]=new SqlParameter("@name",name);
        com_savecookies.Parameters["@time"]=new SqlParameter("@time",time);
        com_savecookies.Parameters["@query"]=new SqlParameter("@query",query);
        com_savecookies.Parameters["@title"]=new SqlParameter("@title",title_tmp);

        com_savecookies.ExecuteNonQuery();
        sc2.Close();

        ////导出搜索记录
        //FileStream aFile = new FileStream(str4 + "data.txt", FileMode.Append);
        //StreamWriter write = new StreamWriter(aFile);
        //write.WriteLine("<user_name>" + Application["user_name"].ToString() + "</user_name><time>" + DateTime.Now.ToString() + "</time><query>" + Application["query"].ToString() + "/<query>" + title_tmp);
        //write.Close();
        string title = "";
        string sort = "";
        //导入文本内容。根据path
        using (StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + path_tmp, System.Text.Encoding.Default))
        {
            title = sr.ReadLine();
            sort = sr.ReadLine();
            text = sr.ReadToEnd();
        }
        //MessageBox.Show(title);
        TextBox1.Text = text;


    }
    /// <summary>
    /// 跳转修改索引页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        Application["title"] = title_tmp;
        Application["path"] = path_tmp;
        Response.Redirect("improveindex.aspx", true);
    }

    protected void ImgButton1_Click(object sender, ImageClickEventArgs e)
    {
        string name=Application["user_name"].ToString();
        SqlConnection sc = new SqlConnection(Application["sqlconnection"].ToString());
        sc.Open();
        SqlCommand com_fav = sc.CreateCommand();
        com_fav.CommandText = "INSERT INTO favorite(Name_Users,Title_Index) VALUES (@name,@title)";
        com_fav.Parameters.Add("@name",SqlDbType.Text);
        com_fav.Parameters.Add("@title", SqlDbType.Text);

        com_fav.Parameters["@name"] = new SqlParameter("@name", name);
        com_fav.Parameters["@title"] = new SqlParameter("@title", title_tmp);
        int count=com_fav.ExecuteNonQuery();
        //if (count == 1)
            //Response.Write("<script language=javascript>alert=('收藏成功！')</script>");

        //else Response.Write("<script language=javascript>alert=('收藏失败！')</script>"); 
        sc.Close();
    }
}